/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import AccesoADatos.GlobalException;
import AccesoADatos.NoDataException;
import AccesoADatos.ServicioLink;
import Logica.Link;
import javax.swing.JOptionPane;

/**
 *
 * @author Fernando
 */
public class CreateServer extends javax.swing.JFrame {

    public CreateServer(Conexion dc, ConectadoGUI sr) {
        initComponents();
        this.setSize(684, 542);
        this.setTitle("Create new Connection.");
        this.data = dc;
        this.svds = sr;



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        esp1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        esp6 = new javax.swing.JTextField();
        esp5 = new javax.swing.JTextField();
        esp4 = new javax.swing.JTextField();
        esp2 = new javax.swing.JTextField();
        esp3 = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("CREATE A NEW CONNECTION");

        jLabel2.setText("Name of the Link:");

        jLabel3.setText("User:");

        jLabel4.setText("Password:");

        jLabel5.setText("Host:");

        jLabel6.setText("Port:");

        jLabel7.setText("Database:");

        esp1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esp1ActionPerformed(evt);
            }
        });

        jButton1.setText("CREATE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        esp6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esp6ActionPerformed(evt);
            }
        });

        esp5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esp5ActionPerformed(evt);
            }
        });

        esp4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esp4ActionPerformed(evt);
            }
        });

        esp2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                esp2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel6)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel5)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel2)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(esp5, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(esp6, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(esp4, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(esp1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(esp3, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(esp2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jButton1)
                        .addGap(0, 0, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(110, 110, 110)
                .addComponent(jLabel1)
                .addGap(0, 304, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(esp1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(esp2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(esp3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(esp4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(esp5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(esp6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addComponent(jButton1)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if ((!(esp1.getText().isEmpty())) && (!(esp5.getText().isEmpty())) && (!(esp3.getText().isEmpty())) && (!(esp4.getText().isEmpty())) && (!(esp5.getText().isEmpty())) && (!(esp6.getText().isEmpty()))) {

            String nl = esp1.getText();
            String us = esp2.getText();
            String ps = esp3.getText();
            String hs = esp4.getText();
            String pt = esp5.getText();
            String db = esp6.getText();
            Link lk = new Link(nl, us, ps, hs, pt, db);
            ServicioLink sl = new ServicioLink(data.user, data.pass, data.ip, data.port, data.db);
            boolean exito = true;
            
            if (exito) {
                try {
                    sl.newDatabaseLink(lk);

                    /*svds.actualizaServers();
                    svds.cargarTablaServers();*/
                    svds.cargarTabla();
                    JOptionPane.showMessageDialog(this, "exito");
                    this.dispose();
                } catch (GlobalException ex) {
                    JOptionPane.showMessageDialog(this, "e1");
                } catch (NoDataException ex) {
                    JOptionPane.showMessageDialog(this, "e2");
                }
            } else {
                JOptionPane.showMessageDialog(this, "You can not put letters in the ip direction.");
            }

        } else {
            JOptionPane.showMessageDialog(this, "There is empty spaces.");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void esp1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esp1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_esp1ActionPerformed

    private void esp6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esp6ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_esp6ActionPerformed

    private void esp5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esp5ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_esp5ActionPerformed

    private void esp4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esp4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_esp4ActionPerformed

    private void esp2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_esp2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_esp2ActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField esp1;
    private javax.swing.JTextField esp2;
    private javax.swing.JPasswordField esp3;
    private javax.swing.JTextField esp4;
    private javax.swing.JTextField esp5;
    private javax.swing.JTextField esp6;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables
    private Conexion data;
    private ConectadoGUI svds;
}
